package com.dianbo.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.AllTypeList;
import util.Constant;
import util.TotalClass;
import util.UtilDAO;

import com.UserModule.bean.TbUserBean;
import com.dati.bean.DatiType;
import com.dianbo.bean.DianboType;
import com.dianbo.dao.DianboTypeDao;
import com.jifen.bean.JiFen;
import com.jifen.dao.JifenDao;

public class DianboTypeServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public DianboTypeServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String method=request.getParameter("method");
		TbUserBean user = (TbUserBean)request.getSession().getAttribute("User");
		if(method.equals("look")){//查询点播的类型
			List list = new AllTypeList().getLastValue(user.getUserId());
			List<DianboType> dianboTypeList = (ArrayList<DianboType>)list.get(0);
			StringBuffer sb = (StringBuffer)list.get(1);
		    request.getSession().setAttribute("result", "");
		    request.getSession().setAttribute("dianboTypeList", dianboTypeList);
		    request.getSession().setAttribute("sb", sb);
		    response.sendRedirect("jifen/jiFenupLoadList.jsp");
		}else if(method.equals("addTypeSubmit")){//新增类型
			String dianboTypeName = Constant.decodeString(request.getParameter("dianboTypeName"));
			boolean boo = new DianboTypeDao().getTypeByName(dianboTypeName);
			if(boo){
				request.getSession().setAttribute("result", "此名称已经存在!");
			}else {
				DianboType dianboType=new DianboType();
				dianboType.setCreateTime(Constant.decodeDate());
				dianboType.setTypeId(TotalClass.getNum());
				dianboType.setTypeName(dianboTypeName);
				dianboType.setUserId(user.getUserId());
				boolean bo = new DianboTypeDao().addDianboType(dianboType);
				if(bo){
					request.getSession().setAttribute("result", "点播类型新建成功!");
				}else{
					request.getSession().setAttribute("result", "点播类型新建失败!");
				}
				
			}
			List list = new AllTypeList().getLastValue(user.getUserId());
			List<DianboType> dianboTypeList = (ArrayList<DianboType>)list.get(0);
			StringBuffer sb = (StringBuffer)list.get(1);
			request.getSession().setAttribute("dianboTypeList", dianboTypeList);
			request.getSession().setAttribute("sb", sb);
			response.sendRedirect("jifen/jiFenupLoadList.jsp");
		}else if(method.equals("modifyType")){//显示要修改的点播类型
			String dianboId = request.getParameter("dianboId");
			String condition=" typeId = '"+dianboId+"' ";
			List<DianboType>  dianboTypeList = new DianboTypeDao().getDianboList("tb_qsType_answer", condition, "createTime");
			DianboType dianboType = new DianboType();
			if(dianboTypeList!=null&&dianboTypeList.size()>0){
				dianboType = dianboTypeList.get(0);
			}
			request.getSession().setAttribute("dianboType", dianboType);
			response.sendRedirect("jifen/modifyDianboType.jsp");
		}else if(method.equals("modifyTypeSubmit")){//修改点播类型提交
			String dianboTypeId = request.getParameter("dianboTypeId");
			String dianboTypeName = Constant.decodeString(request.getParameter("dianboTypeName"));
			DianboType dianboType = new DianboType();
			dianboType.setCreateTime(Constant.decodeDate());
			dianboType.setTypeId(dianboTypeId);
			dianboType.setTypeName(dianboTypeName);
			boolean bo = new DianboTypeDao().modifyDianboType(dianboType);
			if(bo){
				 request.getSession().setAttribute("result", "点播类型修改成功!");
			}else{
				 request.getSession().setAttribute("result", "点播类型修改失败!");
			}
			List list = new AllTypeList().getLastValue(user.getUserId());
			List<DianboType> dianboTypeList = (ArrayList<DianboType>)list.get(0);
			StringBuffer sb = (StringBuffer)list.get(1);
		   
		    request.getSession().setAttribute("dianboTypeList", dianboTypeList);
		    request.getSession().setAttribute("sb", sb);
		    response.sendRedirect("jifen/jiFenupLoadList.jsp");
		}else if(method.equals("del")){
			String dianboTypeId = request.getParameter("dianboTypeId");
			boolean bo = UtilDAO.del("tb_integral", "typeId", dianboTypeId);
			boolean boo =false;
			if(bo){
			      boo = UtilDAO.del("tb_qsType_answer", "typeId", dianboTypeId);
			}
			List list = new AllTypeList().getLastValue(user.getUserId());
			List<DianboType> dianboTypeList = (ArrayList<DianboType>)list.get(0);
			StringBuffer sb = (StringBuffer)list.get(1);
		   
			if(boo){
				request.getSession().setAttribute("result", "点播类型删除成功!");
			}else{
				request.getSession().setAttribute("result", "点播类型删除失败!");
			}
		    request.getSession().setAttribute("dianboTypeList", dianboTypeList);
		    request.getSession().setAttribute("sb", sb);
		    response.sendRedirect("jifen/jiFenupLoadList.jsp");
		}else if(method.equals("lookDown")){//点播下行类型的积分查询
			List list = new AllTypeList().getDownValue(user.getUserId());
			List<DianboType> dianboTypeList = (ArrayList<DianboType>)list.get(0);
			List<DatiType> datiTypeList = (ArrayList<DatiType>)list.get(1);
		    request.getSession().setAttribute("result", "");
		    request.getSession().setAttribute("dianboTypeList", dianboTypeList);
		    request.getSession().setAttribute("datiTypeList", datiTypeList);
		    response.sendRedirect("jifen/jiFendownList.jsp");
		}else if("getValueByTypeId".equals(method)){//根据点播类型的ID得到此类型的分数
			String typeId = request.getParameter("typeId");
			System.out.println("typeId=="+typeId);
			String condition="typeId='"+typeId+"' and integralType=0";//得到此点播类型上行的分数
			JiFen jifen = new JifenDao().getJiFen("tb_integral", "setTime", condition);
			String fenshu = "";
			if(jifen==null){
				fenshu="0";
			}else
			fenshu = jifen.getIntegralValue();
			out.println(fenshu);
		}
	}

	/**
	 * 
	 *@author qingyu zhang
	 *@function:根据登录用户得到其下的所有答题类型
	 * @param userId
	 * @return
	 *2011-4-7
	 *上午09:26:14
	 *zzClub
	 *com.dianbo.servlet
	 *StringBuffer
	 */
	
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-4-4
	 *下午02:41:03
	 *zzClub
	 *com.dianbo.servlet
	 */
}
